<template>
    <div id="zhanghushezhi">
        <div style="width: 500px;height: 600px;margin: auto">
            <div style="text-align: center;margin-bottom: 20px">
                <el-upload
                        class="avatar-uploader"
                        action="http://localhost:8081/zhanghu/uptouxiang"
                        :data="getfileData()"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar" style="border: 1px dashed #d9d9d9;">
                    <i v-else>
                        <img :src="user.touxaing" width="178px" height="200px">
                    </i>
                </el-upload>
                点击上传头像
            </div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名:">
                    <el-input :disabled="true"  v-model="user.username"></el-input>
                </el-form-item>
                <el-form-item label="Email:">
                    <el-input :disabled="true"  v-model="user.Email"></el-input>
                </el-form-item>
                <el-form-item label="旧密码:">
                    <el-input :disabled="true"  v-model="user.password"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Zhanghu",
        data(){
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                user:{
                    username:"",
                    Email:"",
                    password:"",
                    touxiang:""
                },
                imageUrl:"",
                ruleForm: {
                    jiupassword:'',
                    pass: '',
                    checkPass: ''
                },
                filedata:{
                    account:sessionStorage.getItem("user")
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            getfileData(){
                return this.filedata;
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        var s1 = this.ruleForm.checkPass;
                        var s2 = sessionStorage.getItem("user");
                        if(s1!=null&&s2!=null){
                            var params = new URLSearchParams();
                            params.append('account',s2);
                            params.append('password',s1);
                            const Call = this.$http.post("/zhanghu/uppassword",params) .then((res)=>{
                                if(res.data>0){
                                    this.$message({
                                        message: '修改成功',
                                        type: 'success',
                                        duration:2000
                                    })
                                    location.reload();//刷新当前页面
                                }else{
                                    this.$message({
                                        message: '修改失败',
                                        type: 'success',
                                        duration:2000
                                    })
                                }
                            })
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                location.reload();//刷新当前页面
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            //登录成功后根据会话域传来的用户账号
            async cs(){
                var s1 = sessionStorage.getItem("user");
                if(s1!=null){
                    var params = new URLSearchParams();
                    params.append('account',s1);
                    const Call = await this.$http.post("/zhanghu/user",params) .then((res)=>{
                        this.user.touxaing=res.data.avatarUrl
                        this.user.username=res.data.account
                        this.user.Email=res.data.email
                        this.user.password=res.data.password
                    })
                }
            }
        },
        created(){
            this.cs();
        }
    }
</script>

<style scoped>
    #zhanghushezhi{
        width:1300px;
        height:auto;
    }
    .avatar-uploader .el-upload {
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>